package com.shoujifeng.snowmusic.player;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;

import com.shoujifeng.snowmusic.ListView.XListView;
import com.shoujifeng.snowmusic.ListView.XListViewFooter;
import com.shoujifeng.snowmusic.ListView.XListView.IXListViewListener;
import com.shoujifeng.snowmusic.adapter.GalleryAdapter;
import com.shoujifeng.snowmusic.adapter.LoaderAdapter;
import com.shoujifeng.snowmusic.adapter.MyPagerAdapter;
import com.shoujifeng.snowmusic.player.http.GlobalValue;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.utils.LoadMask;
import com.shoujifeng.snowmusic.player.utils.TabHostAnim;

/**
 * 
 * 在线专辑
 * @author luo
 *
 */
public class OnlineSpecialActivity extends Activity implements IXListViewListener{
	private XListView listView;
	private LoaderAdapter mAdapter;
	private Gallery mGallery;
	private GalleryAdapter mGalleryAdapter;
	private final int UPDATE_DATA = 0;
	private final int UPDATE_ONE = 1;
	private ServerAccess serverAccess;
	private ArrayList<HashMap<String, Object>> mTmpList;
	private ArrayList<HashMap<String, Object>> mMusicList;
	private Thread accThread;
	private Handler mHandler;
	private int mPage =  0;
	private View imageView;
	private LoadMask mLoadMask;
	private LinearLayout linearLayout1;
	private LinearLayout linearLayout;
	private ArrayList<HashMap<String, Object>> mAdList;
	
	
	// 翻页控件
		private ViewPager mViewPager;
		// 3个是底部显示当前状态点imageView
		private ImageView mPage0;
		private ImageView mPage1;
		private ImageView mPage2;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.online_special);
		init();
		setListView();
		initGlobalValue();
		XListViewFooter.mContentView.setVisibility(View.GONE);
		mLoadMask = new LoadMask(this);
		mLoadMask.startLoad();
		accThread = new Thread(runnable);
		accThread.start();
		
	}
	
	private void init(){
		mHandler = new Handler();
		serverAccess = new ServerAccess(OnlineSpecialActivity.this);
		listView = (XListView) findViewById(R.id.online_special_listView);
		listView.setPullLoadEnable(true);
		listView.setXListViewListener(this);
		listView.setOnItemClickListener(itemClickListener);
		
		imageView  = LayoutInflater.from(OnlineSpecialActivity.this)
				.inflate(R.layout.image_view, null);
		listView.addHeaderView(imageView);
		
	}
	private void setListView() {
		mMusicList = new ArrayList<HashMap<String, Object>>();
		String keys[] = new String[] { ServerAccess.ALBUM,
				ServerAccess.LISTENER, ServerAccess.SINGER };
		mAdapter = new LoaderAdapter(this, mMusicList, new int[] {
				R.id.special_name, R.id.countTxt, R.id.playerName }, keys,
				new String[] { ServerAccess.ICON_URL },
				new int[] { R.id.face }, R.drawable.icon_album_default,
				R.layout.online_special_list);
		listView.setAdapter(mAdapter);
	}
	private void creatImg(){
		linearLayout1 = (LinearLayout) imageView.findViewById(R.id.linearLayout1);
		for(int i = 0;i<mAdList.size();i++){
			linearLayout = new LinearLayout(OnlineSpecialActivity.this);
			ImageView image = new ImageView(OnlineSpecialActivity.this);
			linearLayout.setPadding(5, 5, 5, 5);	
			if(i==0){
				image.setBackgroundResource(R.drawable.no);
			}
			else{
				image.setBackgroundResource(R.drawable.on);
			}
			linearLayout.addView(image);
			
			linearLayout1.addView(linearLayout);
		}
	}
	// 设定画廊的显示方式和内容
		public void prepare() {
			
			mGallery = (Gallery) imageView.findViewById(R.id.home_gallery);
			mGalleryAdapter = new GalleryAdapter(this,mAdList);
			mGallery.setAdapter(mGalleryAdapter);
			mGallery.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Intent intent = new Intent(OnlineSpecialActivity.this,OnlineAdvContentActivity.class);
					intent.putExtra(ServerAccess.ID, mAdList.get(position).get(ServerAccess.ID).toString());
					startActivity(intent);
					
					TabHostAnim.set(R.anim.right_in, R.anim.no_move);
				}

			});
			mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					//mMyView.setSeletion(arg2);
					linearLayout.removeAllViews();
					linearLayout1.removeAllViews();
						for(int i = 0;i<mAdList.size();i++){
							linearLayout = new LinearLayout(OnlineSpecialActivity.this);
							ImageView image = new ImageView(OnlineSpecialActivity.this);
							linearLayout.setPadding(5, 5, 5, 5);	
							if(i==arg2){
								image.setBackgroundResource(R.drawable.no);
							}
							else{
								image.setBackgroundResource(R.drawable.on);
							}
							linearLayout.addView(image);
							
							linearLayout1.addView(linearLayout);
						}
					
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {

				}
			});
		}
	
	 private OnItemClickListener itemClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long arg3) {
				// 此处相应ListView 项按下
				// pos是按下项的ID

				Intent intent = new Intent(OnlineSpecialActivity.this, OnlineSpecialContentActivity.class);
				intent.putExtra(ServerAccess.ALBUM,mMusicList.get(position-1).get(ServerAccess.ALBUM).toString());
				intent.putExtra(ServerAccess.INDEX,mMusicList.get(position-1).get(ServerAccess.INDEX).toString());
				intent.putExtra(ServerAccess.SINGER,mMusicList.get(position-1).get(ServerAccess.SINGER).toString());
				startActivity(intent);
				
				TabHostAnim.set(R.anim.right_in, R.anim.no_move);
			}
		};
		private void initGlobalValue() {
			GlobalValue.initSDAddr();

			GlobalValue.addUrl(ServerAccess.MUSIC_ALBUM,
					"http://www.snowmusic.com.cn/mobile_api/music_album.php");
		}

		protected Handler handler = new Handler() {
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {

				switch (msg.what) {
				case UPDATE_DATA: {
					mLoadMask.stopLoad();
					XListViewFooter.mContentView.setVisibility(View.VISIBLE);
					updateArrayList();
				}
					break;
				case UPDATE_ONE:{
					creatImg();
					prepare();
				}
					break;

				default:
					break;
				}
				super.handleMessage(msg);
			}
		};

		// 创建多线程获取数据
		protected Runnable runnable = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					   String[] keys = new String[] { ServerAccess.ALBUM,
								ServerAccess.LISTENER, ServerAccess.SINGER,
								ServerAccess.ICON_URL ,ServerAccess.INDEX};
						mTmpList = new ArrayList<HashMap<String, Object>>();
						mTmpList = serverAccess.getSpecialListOfIndex(keys,mPage);
						ServerAccess.netListCount = mTmpList.size();
						sendMessage(UPDATE_DATA);
					
						mAdList = new ArrayList<HashMap<String,Object>>();
						String keys2[] = new String[] {ServerAccess.ID,ServerAccess.MID,ServerAccess.STATE,ServerAccess.ICON_URL,ServerAccess.CODE};
						mAdList = serverAccess.getListAdv("3",keys2);
						sendMessage(UPDATE_ONE);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		};

		// 发送消息到Handler
		private void sendMessage(int what) {
			Message msg = new Message();
			msg.what = what;
			handler.sendMessage(msg);
		}

		// 若在线程中更新与Adapter的动态数组将会产生致命错误。
		private void updateArrayList() {

			if (mMusicList != null) {
				int count = mTmpList.size();
				for (int i = 0; i < count; i++) {
					mMusicList.add(mTmpList.get(i));
				}

				 mTmpList.clear();
				 mTmpList = null;
			}
			mAdapter.notifyDataSetChanged();
		}
		
		public class MyOnPageChangeListener implements OnPageChangeListener {

			public void onPageSelected(int page) {
				// 翻页时当前page,改变当前状态园点图片
				switch (page) {
				case 0:
					mPage0.setImageDrawable(getResources().getDrawable(
							R.drawable.on));
					mPage1.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					mPage2.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					break;
				case 1:
					mPage0.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					mPage1.setImageDrawable(getResources().getDrawable(
							R.drawable.on));
					mPage2.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					break;
				case 2:
					mPage0.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					mPage1.setImageDrawable(getResources().getDrawable(
							R.drawable.no));
					mPage2.setImageDrawable(getResources().getDrawable(
							R.drawable.on));
					break;

				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
}

		private void onLoad() {
			listView.stopRefresh();
			listView.stopLoadMore();
			listView.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		}
		@Override
		public void onLoadMore() {
			// TODO Auto-generated method stub
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						mPage = mPage+1;
						accThread = new Thread(runnable);
						accThread.start();
						onLoad();
					}
				}, 1000);
			}
			
		
}
